package com.bolingcavalry.crm.taskorder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.taskorder.service.ConfirmationService;
import com.dto.ConfirmationOrderDTO;
import com.vo.ConfirmationOrderVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api("确认单管理")
@RestController
@RequestMapping("/confirm")
@Slf4j
public class ConfirmationController {
    @Autowired
    private ConfirmationService confirmationService;

    /**
     * 新增确认单
     * @param confirmationOrderDTO
     * @return
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody ConfirmationOrderDTO confirmationOrderDTO){
        return confirmationService.add(confirmationOrderDTO);
    }

    //修改确认单
    @PostMapping("/update")
    public ResponseResult update(@RequestBody ConfirmationOrderDTO confirmationOrderDTO){
        return confirmationService.update(confirmationOrderDTO);
    }

    /**
     * 上传确认单图片并识别内容并返回VO
     * @throws IOException
     */
    @PostMapping("/updateByImage")
    public ResponseResult updateByImage(@RequestParam("image") MultipartFile image) throws Exception {
        ConfirmationOrderVO s = confirmationService.updateByImage(image);
        return ResponseResult.success(s);
    }

    //批量逻辑删除
    @DeleteMapping("/batchdelete")
    public ResponseResult batchDelete(@RequestBody String[] ids){
        if(confirmationService.batchDelete(ids))
        return ResponseResult.success("删除成功");
        return ResponseResult.error("删除失败,下级存在订单请先删除对应的下级订单");
    }
    //getbyid
    @GetMapping("/get/{id}")
    public ResponseResult getById(@PathVariable Long id) throws Exception {
        return ResponseResult.success(confirmationService.getByTaskOrderId(id));
    }
    //根据售后编号获取确认单详细
    @GetMapping("/getByServiceNumber/{orderNumber}")
    public ResponseResult<ConfirmationOrderVO> getByServiceNumber(@PathVariable String orderNumber) throws Exception {
        return ResponseResult.success(confirmationService.getByServiceNumber(orderNumber));
    }

    //分页条件查询
    @GetMapping("/list")
    public  ResponseResult<IPage<ConfirmationOrderVO>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String customerName, // 客户公司名（用于模糊查询）
            @RequestParam(required = false) String taskOrderNumber, // 订单编号
            @RequestParam(required = false) String startTime, // 开始时间
            @RequestParam(required = false) String endTime, // 结束时间
            @RequestParam(required = false) Integer status // 状态
    )
        {
            IPage<ConfirmationOrderVO>  page = confirmationService.getByPage(pageNum, pageSize, customerName, taskOrderNumber, startTime, endTime, status);
            PageResult<ConfirmationOrderVO> result = new PageResult<>();
            result.setTotal(page.getTotal());
            result.setPages((int) page.getPages());
            result.setCurrent((int) page.getCurrent());
            result.setSize((int) page.getSize());
            result.setRecords(page.getRecords());
            return ResponseResult.success("查询成功",result);
        }

    //改变状态
    @PostMapping("/updateStatus")
    public ResponseResult updateStatus(
            @RequestBody ConfirmationOrderDTO confirmationOrderDTO){
        confirmationService.updateStatus(confirmationOrderDTO);
        return ResponseResult.success("状态修改成功");
    }
}
